package com.atguigu.tingshu.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    //自定义线程池+并行实现
    //专辑详情
    @Override
    public Map<String, Object> getItem(Long albumId) {
        Map<String, Object> map = new HashMap<String, Object>();
        //1 根据专辑id获取专辑信息
        CompletableFuture<AlbumInfo> completableFuture1 =
                                  CompletableFuture.supplyAsync(() -> {
            Result<AlbumInfo> albumInfoResult =
                    albumInfoFeignClient.getAlbumInfo(albumId);
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "专辑数据为空");
            //封装专辑信息到map集合
            map.put("albumInfo", albumInfo);
            return albumInfo;
        });

        //2 根据专辑里面三级id获取一级和二级分类数据
        CompletableFuture<Void> completableFuture2 =
                   completableFuture1.thenAcceptAsync(albumInfo -> {
            //获取专辑里面三级id
            Long category3Id = albumInfo.getCategory3Id();
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.getCategoryView(category3Id);
            BaseCategoryView baseCategoryView = categoryViewResult.getData();
            Assert.notNull(baseCategoryView, "分类为空");
            //封装map
            map.put("baseCategoryView", baseCategoryView);
        });

        //3 根据专辑id获取四个统计数据
        CompletableFuture<Void> completableFuture3 = CompletableFuture.runAsync(() -> {
            Result<AlbumStatVo> albumStatVoResult =
                    albumInfoFeignClient.getAlbumStatVo(albumId);
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            Assert.notNull(albumStatVo, "统计数据为空");
            //封装map
            map.put("albumStatVo", albumStatVo);
        });

        //4 根据用户id获取用户信息
        CompletableFuture<Void> completableFuture4 =
                         completableFuture1.thenAcceptAsync(albumInfo -> {
            Result<UserInfoVo> userInfoVoResult =
                       userInfoFeignClient.getUserInfoVo(albumInfo.getUserId());
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            Assert.notNull(userInfoVo, "用户数据为空");
            map.put("announcer", userInfoVo);
        });

        //5 多个任务都执行完成汇总
        CompletableFuture.allOf(
                completableFuture1,
                completableFuture2,
                completableFuture3,
                completableFuture4
        ).join();
        //返回map集合
        return map;
    }
}
